import { defineConfig, loadEnv } from 'vite';
import { svgSpritemap } from 'vite-plugin-svg-spritemap';
import vue from '@vitejs/plugin-vue';
import qiankun from 'vite-plugin-qiankun';

const target = 'https://www.yunfeng.icu';
// const target = 'http://127.0.0.1:9098';

export default defineConfig((mode) => {
  const env = loadEnv(mode.mode, process.cwd());
  return {
    base: `/${env.VITE_BASE}`, // 和基座中配置的activeRule一致
    resolve: {
      alias: {
        '@': '/src'
      }
    },
    plugins: [
      vue(),
      svgSpritemap({
        pattern: 'src/assets/icons/*.svg',
        prefix: 'icon',
        currentColor: true,
        filename: env.VITE_SPRITEMAP
      }),
      qiankun(env.VITE_BASE, { // 配置qiankun插件
        useDevMode: true
      })
    ],
    build: {
      outDir: `../../dist/html/${env.VITE_BASE}`
    },
    server: {
      port: Number(env.VITE_PORT),
      open: true,
      // 需要设置为 true, 否则开发环境下子应用资源无法在主应用展示
      cors: true,
      origin: `${env.VITE_HOST}:${env.VITE_PORT}`,
      proxy: {
        '/api': {
          target,
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/api/, ''),
        },
      }
    },
  }
})
